using SharpDX;
using System.IO;

namespace Engine.FileFormats3D.C11Undercover
{
    internal class C11Vertex
    {
        #region Private Fields

        private Color4 diffuseColor;
        private int normalX, normalY, normalZ, normal0;
        private float textureU1, textureV1, textureU2, textureV2;
        private Color4 vertexColor;
        private float x, y, z;

        #endregion Private Fields

        #region Public Properties

        public Color4 DiffuseColor
        {
            get { return diffuseColor; }
            set { diffuseColor = value; }
        }

        public int Normal0
        {
            get { return normal0; }
            set { normal0 = value; }
        }

        public int NormalX
        {
            get { return normalX; }
            set { normalX = value; }
        }

        public int NormalY
        {
            get { return normalY; }
            set { normalY = value; }
        }

        public int NormalZ
        {
            get { return normalZ; }
            set { normalZ = value; }
        }

        public float TextureU1
        {
            get { return textureU1; }
            set { textureU1 = value; }
        }

        public float TextureU2
        {
            get { return textureU2; }
            set { textureU2 = value; }
        }

        public float TextureV1
        {
            get { return textureV1; }
            set { textureV1 = value; }
        }

        public float TextureV2
        {
            get { return textureV2; }
            set { textureV2 = value; }
        }

        public Color4 VertexColor
        {
            get { return vertexColor; }
            set { vertexColor = value; }
        }

        public float X
        {
            get { return x; }
            set { x = value; }
        }

        public float Y
        {
            get { return y; }
            set { y = value; }
        }

        public float Z
        {
            get { return z; }
            set { z = value; }
        }

        #endregion Public Properties

        #region Public Methods

        public override string ToString()
        {
            return "vertex Normal0: " + this.Normal0.ToString();
        }

        #endregion Public Methods

        #region Internal Methods

        internal static C11Vertex FromStream(BinaryReader br)
        {
            C11Vertex vertex = new C11Vertex();

            vertex.X = br.ReadSingle();
            vertex.Y = br.ReadSingle();
            vertex.Z = br.ReadSingle();
            vertex.NormalX = (int)br.ReadByte() - 128;
            vertex.NormalY = (int)br.ReadByte() - 128;
            vertex.NormalZ = (int)br.ReadByte() - 128;
            vertex.Normal0 = br.ReadByte();
            vertex.TextureU1 = br.ReadSingle();
            vertex.TextureV1 = br.ReadSingle();
            vertex.TextureU2 = br.ReadSingle();
            vertex.TextureV2 = br.ReadSingle();

            vertex.VertexColor = new Color4((int)br.ReadUInt32());
            //vertex.VertexColor = new Color4()
            //{
            //    Red = br.ReadByte(),
            //    Green = br.ReadByte(),
            //    Blue = br.ReadByte()
            //    Alpha = br.ReadByte(), // Shadow
            //};

            vertex.DiffuseColor = new Color4((int)br.ReadUInt32());
            //vertex.DiffuseColor = new Color4()
            //{
            //    Alpha = br.ReadByte() / 255.0f,
            //    Red = br.ReadByte() / 255.0f,
            //    Green = br.ReadByte() / 255.0f,
            //    Blue = br.ReadByte() / 255.0f
            //};

            //// For testing
            //vertex.VertexColor = new Color4()
            //{
            //    Red = 1,
            //    Green = 0.66f,
            //    Blue = 0.33f,
            //    Alpha = 0,
            //};
            //vertex.DiffuseColor = new Color4()
            //{
            //    Red = 1,
            //    Green = 0.66f,
            //    Blue = 0.33f,
            //    Alpha = 0,
            //};

            //vertex.normalX = 0;
            //vertex.normalY = 0;
            //vertex.normalZ = 0;
            //vertex.normal0 = 0;

            return vertex;
        }

        #endregion Internal Methods
    }
}